#include "asr.h"
extern "C"{ void * __dso_handle = 0 ;}
#include "setup.h"
#include "myLib/asr_ds1302.h"
#include "myLib/asr_dhtxx.h"
#include "HardwareSerial.h"
#include "myLib/asr_hwtimer.h"
#include "asr_math.h"
#include "myLib/asr_event.h"

uint32_t snid;
uint8_t wet = 0;
uint8_t temp = 0;
uint8_t hour = 0;
uint8_t min2 = 0;
uint8_t day = 0;
uint8_t year = 0;
uint8_t month = 0;
uint8_t date = 0;
uint8_t minn = 0;
bool alarm_s = 0;
uint8_t hour_a;
uint8_t min_a;
SYSTEMTIME TIME_RTC;
DS1302 ds1302(4,6,5);
uint8_t sys1_0[9]={0x73,0x79,0x73,0x31,0x3D,0x30,0xff,0xff,0xff};
uint8_t sys0_1[9]={0x73,0x79,0x73,0x30,0x3D,0x31,0xff,0xff,0xff,};
uint8_t sys0_0[9]={0x73,0x79,0x73,0x30,0x3D,0x30,0xff,0xff,0xff,};
uint8_t page1[10]={0x73,0x6C,0x65,0x65,0x70,0x3D,0x30,0xff,0xff,0xff};
uint8_t page0[10]={0x73,0x6C,0x65,0x65,0x70,0x3D,0x31,0xff,0xff,0xff};
uint8_t count[18]={0x70,0x61,0x67,0x65,0x31,0x2E,0x6E,0x31,0x2E,0x76,0x61,0x6C,0x3D,0x32,0x34,0xff,0xff,0xff};
QueueHandle_t asr_code_handler_msg=NULL;
void asr_code_handler_app();
void app1();
void app();
void alarm();


//{playid:10001,voice:}
//{playid:10002,voice:}

//{ID:10250,keyword:"命令词",ASR:"最大音量",ASRTO:""}
//{ID:10251,keyword:"命令词",ASR:"中等音量",ASRTO:""}
//{ID:10252,keyword:"命令词",ASR:"最小音量",ASRTO:""}
DHTxx asr_dht_20(20, DHT11);
void TIM_attachInterrupt_0() {
  ds1302.read_rtc(&TIME_RTC);
  min2 = TIME_RTC.Minute;
  if(minn != min2){
    year = (TIME_RTC.Year)%100;
    month = TIME_RTC.Month;
    day = TIME_RTC.Week;
    date = TIME_RTC.Day;
    hour = TIME_RTC.Hour;
    count[(int)(7)] = 0x30;
    count[(int)(13)] = 0x30+(hour/10%10);
    count[(int)(14)] = 0x30+(hour%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    count[(int)(7)] = 0x31;
    count[(int)(13)] = 0x30+(min2/10%10);
    count[(int)(14)] = 0x30+(min2%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    count[(int)(7)] = 0x32;
    count[(int)(13)] = 0x30+(year/10%10);
    count[(int)(14)] = 0x30+(year%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    count[(int)(7)] = 0x33;
    count[(int)(13)] = 0x30+(month/10%10);
    count[(int)(14)] = 0x30+(month%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    count[(int)(7)] = 0x34;
    count[(int)(13)] = 0x30+(date/10%10);
    count[(int)(14)] = 0x30+(date%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    count[(int)(7)] = 0x35;
    count[(int)(13)] = 0x30;
    if(day == 7){
      count[(int)(14)] = 0x38;
    }
    else{
      count[(int)(14)] = 0x30+day%10;
    }
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    minn = min2;
  }
  count[(int)(7)] = 0x30;
  count[(int)(13)] = 0x30+(hour/10%10);
  count[(int)(14)] = 0x30+(hour%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x31;
  count[(int)(13)] = 0x30+(min2/10%10);
  count[(int)(14)] = 0x30+(min2%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
}

void asr_code_handler_app(){
  uint16_t asr_code_handler_snid;
  while (1) {
    if(xQueueReceive(asr_code_handler_msg,&asr_code_handler_snid,0)){
      switch (asr_code_handler_snid) {
        case 100:
          play_audio((random(20, 22+1)));
          for (int i = 0; i < 9; i = i + 1) {
            Serial1.write(sys0_1[(int)(i)]);
          }
          break;
        case 98:
          for (int i = 0; i < 9; i = i + 1) {
            Serial1.write(sys0_1[(int)(i)]);
          }
          break;
        case 99:
          for (int i = 0; i < 10; i = i + 1) {
            Serial1.write(page0[(int)(i)]);
          }
          delay(1000);
          exit_wakeup_deal(0);
          break;
        case 0:
          for (int i = 0; i < 10; i = i + 1) {
            Serial1.write(page1[(int)(i)]);
          }
          delay(1000);
          play_audio(19);
          break;
        case 97:
          ds1302.read_rtc(&TIME_RTC);
          hour = TIME_RTC.Hour;
          min2 = TIME_RTC.Minute;
          for (int i = 0; i < 13; i = i + 1) {
            Serial1.write(sys0_1[(int)(i)]);
          }
          play_audio(11);
          if(hour/10%10 != 0){
            if(hour/10%10 != 1){
              play_audio(hour/10%10);
            }
            play_audio(10);
            if(hour%10 != 0){
              play_audio(hour/1%10);
            }
          }
          else{
            play_audio(hour%10);
          }
          play_audio(12);
          if(min2/10%10 != 0){
            if(min2/10%10 != 1){
              play_audio(min2/10%10);
            }
            play_audio(10);
            if(min2%10 != 0){
              play_audio(min2/1%10);
            }
          }
          else{
            play_audio(min2%10);
          }
          play_audio(13);
          break;
        case 96:
          ds1302.read_rtc(&TIME_RTC);
          month = TIME_RTC.Month;
          date = TIME_RTC.Day;
          day = TIME_RTC.Week;
          for (int i = 0; i < 13; i = i + 1) {
            Serial1.write(sys0_1[(int)(i)]);
          }
          play_audio(14);
          if(month/10%10 != 0){
            if(month/10%10 != 1){
              play_audio(month/10%10);
            }
            play_audio(10);
            if(month%10 != 0){
              play_audio(month/1%10);
            }
          }
          else{
            play_audio(month%10);
          }
          play_audio(15);
          if(date/10%10 != 0){
            if(date/10%10 != 1){
              play_audio(date/10%10);
            }
            play_audio(10);
            if(date%10 != 0){
              play_audio(date/1%10);
            }
          }
          else{
            play_audio(date%10);
          }
          play_audio(16);
          if(day == 7){
            play_audio(17);
          }
          else{
            play_audio(day);
          }
          break;
        case 95:
          temp = asr_dht_20.read_temperature(0);
          delay(2000);
          wet = asr_dht_20.readHumidity();
          for (int i = 0; i < 13; i = i + 1) {
            Serial1.write(sys0_1[(int)(i)]);
          }
          if(temp/10%10 != 0){
            if(temp/10%10 != 1){
              play_audio(temp/10%10);
            }
            play_audio(10);
            if(temp%10 != 0){
              play_audio(temp/1%10);
            }
          }
          else{
            play_audio(temp%10);
          }
          play_audio(18);
          if(wet/10%10 != 0){
            if(wet/10%10 != 1){
              play_audio(wet/10%10);
            }
            play_audio(10);
            if(wet%10 != 0){
              play_audio(wet/1%10);
            }
          }
          else{
            play_audio(wet%10);
          }
          break;
      }
    }
    delay(1);
  }
  vTaskDelete(NULL);
}

void app1(){
  while (1) {
    temp = asr_dht_20.read_temperature(0);
    count[(int)(7)] = 0x37;
    count[(int)(13)] = 0x30+(wet/10%10);
    count[(int)(14)] = 0x30+(wet%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    delay(2000);
    wet = asr_dht_20.readHumidity();
    count[(int)(7)] = 0x36;
    count[(int)(13)] = 0x30+(temp/10%10);
    count[(int)(14)] = 0x30+(temp%10);
    for (int i = 0; i < 18; i = i + 1) {
      Serial1.write(count[(int)(i)]);
    }
    delay(2000);
  }
  vTaskDelete(NULL);
}

void app(){
  while (1) {
    String Rec = "";
    Rec = Serial1.readString();
    if((uint8_t)Rec[0] == 0xEF){
      if((uint8_t)Rec[1] == 0x02){
        delay(200);
        enter_wakeup(15000);
        delay(200);
        play_audio((random(20, 22+1)));
      }
      Rec = "";
    }
    else if((uint8_t)Rec[0] == 0x68){
      if((uint8_t)Rec[1] == 0x00){
        delay(200);
        enter_wakeup(15000);
        delay(200);
        play_audio(19);
      }
      Rec = "";
    }
    else if((uint8_t)Rec[0] == 0xAB){
      delay(200);
      enter_wakeup(15000);
      delay(200);
      hour_a = (uint8_t)Rec[1];
      min_a = (uint8_t)Rec[2];
      alarm_s = 1;
    }
    else if((uint8_t)Rec[0] == 0xAC){
      delay(200);
      enter_wakeup(15000);
      delay(200);
      ds1302.set_rtc((uint8_t)Rec[1],(uint8_t)Rec[2],(uint8_t)Rec[3],(uint8_t)Rec[6]);
      ds1302.set_rtc((uint8_t)Rec[4],(uint8_t)Rec[5],0);
      ds1302.read_rtc(&TIME_RTC);
      year = (TIME_RTC.Year)%100;
      month = TIME_RTC.Month;
      day = TIME_RTC.Week;
      date = TIME_RTC.Day;
      hour = TIME_RTC.Hour;
      count[(int)(7)] = 0x32;
      count[(int)(13)] = 0x30+(year/10%10);
      count[(int)(14)] = 0x30+(year%10);
      for (int i = 0; i < 18; i = i + 1) {
        Serial1.write(count[(int)(i)]);
      }
      count[(int)(7)] = 0x33;
      count[(int)(13)] = 0x30+(month/10%10);
      count[(int)(14)] = 0x30+(month%10);
      for (int i = 0; i < 18; i = i + 1) {
        Serial1.write(count[(int)(i)]);
      }
      count[(int)(7)] = 0x34;
      count[(int)(13)] = 0x30+(date/10%10);
      count[(int)(14)] = 0x30+(date%10);
      for (int i = 0; i < 18; i = i + 1) {
        Serial1.write(count[(int)(i)]);
      }
      count[(int)(7)] = 0x35;
      count[(int)(13)] = 0x30;
      if(day == 7){
        count[(int)(14)] = 0x38;
      }
      else{
        count[(int)(14)] = 0x30+day%10;
      }
      for (int i = 0; i < 18; i = i + 1) {
        Serial1.write(count[(int)(i)]);
      }
    }
    delay(2);
  }
  vTaskDelete(NULL);
}

void alarm(){
  while (1) {
    if(alarm_s){
      if((hour == hour_a) && (min2 == min_a)){
        delay(200);
        enter_wakeup(15000);
        delay(200);
        for (int i = 0; i < 10; i = i + 1) {
          Serial1.write(page1[(int)(i)]);
        }
        for (int i = 0; i < 9; i = i + 1) {
          Serial1.write(sys1_0[(int)(i)]);
        }
        play_audio(25);
        alarm_s = 0;
      }
    }
    delay(2);
  }
  vTaskDelete(NULL);
}

void ASR_CODE()
{
  if(snid == 100){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 98){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 99){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 0){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 97){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 96){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
  if(snid == 95){
    xQueueSend(asr_code_handler_msg,&snid,0);
  }
}

void hardware_init(){
  vol_set(5);
  asr_dht_20.init();
  setPinFun(2,FORTH_FUNCTION);
  setPinFun(3,FORTH_FUNCTION);
  Serial1.begin(9600);
  for (int i = 0; i < 13; i = i + 1) {
    Serial1.write(page1[(int)(i)]);
  }
  delay(1000);
  delay(200);
  enter_wakeup(15000);
  delay(200);
  ds1302.read_rtc(&TIME_RTC);
  min2 = TIME_RTC.Minute;
  year = (TIME_RTC.Year)%100;
  month = TIME_RTC.Month;
  day = TIME_RTC.Week;
  date = TIME_RTC.Day;
  hour = TIME_RTC.Hour;
  count[(int)(7)] = 0x30;
  count[(int)(13)] = 0x30+(hour/10%10);
  count[(int)(14)] = 0x30+(hour%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x31;
  count[(int)(13)] = 0x30+(min2/10%10);
  count[(int)(14)] = 0x30+(min2%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x32;
  count[(int)(13)] = 0x30+(year/10%10);
  count[(int)(14)] = 0x30+(year%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x33;
  count[(int)(13)] = 0x30+(month/10%10);
  count[(int)(14)] = 0x30+(month%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x34;
  count[(int)(13)] = 0x30+(date/10%10);
  count[(int)(14)] = 0x30+(date%10);
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  count[(int)(7)] = 0x35;
  count[(int)(13)] = 0x30;
  if(day == 7){
    count[(int)(14)] = 0x38;
  }
  else{
    count[(int)(14)] = 0x30+day%10;
  }
  for (int i = 0; i < 18; i = i + 1) {
    Serial1.write(count[(int)(i)]);
  }
  asr_code_handler_msg=xQueueCreate(5,4);
  xTaskCreate(app1,"app1",128,NULL,5,NULL);
  xTaskCreate(app,"app",256,NULL,4,NULL);
  xTaskCreate(alarm,"alarm",128,NULL,4,NULL);
  vTaskDelete(NULL);
}

void setup()
{
  TIM_attachInterrupt(TIMER0, 1000000, TIM_attachInterrupt_0);
  set_state_enter_wakeup(15000);
  //{ID:0,keyword:"唤醒词",ASR:"兔兔",ASRTO:""}
  //{ID:100,keyword:"命令词",ASR:"你好",ASRTO:""}
  //{ID:99,keyword:"命令词",ASR:"再见",ASRTO:""}
  //{ID:98,keyword:"命令词",ASR:"可以后空翻吗",ASRTO:"23"}
  //{ID:97,keyword:"命令词",ASR:"几点啦",ASRTO:""}
  //{ID:96,keyword:"命令词",ASR:"今天几号",ASRTO:""}
  //{ID:95,keyword:"命令词",ASR:"天气如何",ASRTO:""}
  //{playid:1,voice:1}
  //{playid:2,voice:2}
  //{playid:3,voice:3}
  //{playid:4,voice:4}
  //{playid:5,voice:5}
  //{playid:6,voice:6}
  //{playid:7,voice:7}
  //{playid:8,voice:8}
  //{playid:9,voice:9}
  //{playid:10,voice:10}
  //{playid:11,voice:现在是}
  //{playid:12,voice:点}
  //{playid:13,voice:分}
  //{playid:14,voice:今天是}
  //{playid:15,voice:月}
  //{playid:16,voice:日星期}
  //{playid:17,voice:天}
  //{playid:18,voice:度 湿度百分之}
  //{playid:19,voice:（唤醒语音）}
  //{playid:20,voice:随1}
  //{playid:21,voice:随2}
  //{playid:22,voice:随3}
  //{playid:23,voice:}
  //{playid:24,voice:随4}
  //{playid:25,voice:博士，该起床了哦，太阳都晒到屁股啦，早饭已经做好啦，今天还有很多工作需要博士处理呢，继续加油哦}
  ds1302.init();
  xTaskCreate(asr_code_handler_app,"asr_code_handler_app",256,NULL,4,NULL);
}
